/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.process;

import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.DomainVO;
import cn.com.jsepc.rep.vo.LifecycleVO;
import cn.com.jsepc.rep.vo.LinkVO;
import cn.com.jsepc.rep.vo.PointVO;
import cn.com.jsepc.rep.vo.ProcessVO;
import com.jarp.db.DAO;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;

/**
 *
 * @author 赵远
 */
public class GetTree {
    public String gettree(){
         String res="";
        String sql="select * from tb_domain";
        DAO dao=new DAO();
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            List<DomainVO> iDomain=new ArrayList<DomainVO>();
            if(v==null){
                
            }else{
            for(int i=0;i<v.size();i++){
                Hashtable hashRows = (Hashtable) v.get(i);
                DomainVO domain=new DomainVO();
                domain.setDomainDesc((String)hashRows.get("DOMAINDESC"));
                domain.setId("domain_"+(String)hashRows.get("DOMAINID"));
                domain.setText((String)hashRows.get("DOMAINNAME"));
                domain.setLeaf(this.isleaf(domain.getId(), "tb_process", "DOMAINID"));
                iDomain.add(domain);
            }
            JSONArray json = new JSONArray();
                json = JSONArray.fromObject(iDomain);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public String getProcess(String domainID){
        String res="";
       String [] myid=domainID.split("_");
        domainID=myid[myid.length-1];
        String sql="select * from tb_process where domainid="+domainID;
        DAO dao=new DAO();
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            List<ProcessVO> iProcess=new ArrayList<ProcessVO>();
            if(v==null){
                
            }else{
            for(int i=0;i<v.size();i++){
                Hashtable hashRows = (Hashtable) v.get(i);
                ProcessVO process=new ProcessVO();
                process.setProcessCode((String)hashRows.get("PROCESSCODE"));
                process.setProcessDesc((String)hashRows.get("PROCESSDESC"));
                process.setId("pro_"+(String)hashRows.get("PROCESSID"));
                process.setText((String)hashRows.get("PROCESSNAME"));
                process.setLeaf(this.isleaf(process.getId(), "tb_process_lifecycle", "PROCESSID"));
                iProcess.add(process);
            }
            JSONArray json = new JSONArray();
                json = JSONArray.fromObject(iProcess);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public String getLifecycle(String processID){
        String [] myid=processID.split("_");
        processID=myid[myid.length-1];
        String res=""; 
        String sql="select * from tb_process_lifecycle  where processid="+processID;
        DAO dao=new DAO();
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                
            }
            else{
            List<LifecycleVO> iList=new ArrayList<LifecycleVO>();
            for(int i=0;i<v.size();i++){
                Hashtable hashRows = (Hashtable) v.get(i);
                LifecycleVO life=new LifecycleVO();
                life.setLifecycleDesc((String)hashRows.get("LIFECYCLEDESC"));
                life.setId("life_"+(String)hashRows.get("LIFECYCLEID"));
                life.setText((String)hashRows.get("LIFECYCLENAME"));
               life.setLeaf(this.isleaf(life.getId(), "tb_link", "LIFECYCLEID"));
                iList.add(life);
            }
                 JSONArray json = new JSONArray();
                json = JSONArray.fromObject(iList);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public String getList(String lifecycleID){
        String [] myid=lifecycleID.split("_");
        lifecycleID=myid[myid.length-1];
        String res="";
        String sql="select * from tb_link where lifecycleid="+lifecycleID;
        DAO dao=new DAO();
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                
            }
            else{
            List<LinkVO> iList=new ArrayList<LinkVO>();
            for(int i=0;i<v.size();i++){
                Hashtable hashRows = (Hashtable) v.get(i);
                LinkVO link=new LinkVO();
                link.setLinkDesc((String)hashRows.get("LINKDESC"));
                link.setLinkcode((String)hashRows.get("LINKCODE"));
                link.setId("link_"+(String)hashRows.get("LINKID"));
                link.setText((String)hashRows.get("LINKNAME"));
                link.setLeaf(this.isleaf(link.getId(), "tb_point", "LINKID"));
                iList.add(link);
            }
                 JSONArray json = new JSONArray();
                json = JSONArray.fromObject(iList);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public String getPoint(String linkid){
         String [] myid=linkid.split("_");
        linkid=myid[myid.length-1];
        String res="";
        String sql="select * from tb_point where linkid="+linkid;
        DAO dao=new DAO();
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v==null){
                
            }
            else{
           List<PointVO> iPoint=new ArrayList<PointVO>();
            for(int i=0;i<v.size();i++){
                Hashtable hashRows = (Hashtable) v.get(i);
                PointVO point=new PointVO();
                point.setPointCode((String)hashRows.get("POINTCODE"));
                point.setPointDesc((String)hashRows.get("POINTDESC"));
                point.setId("point_"+(String)hashRows.get("POINTID"));
                point.setText((String)hashRows.get("POINTNAME"));
                point.setLeaf(true);
                iPoint.add(point);
            }
            JSONArray json = new JSONArray();
                json = JSONArray.fromObject(iPoint);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    public boolean isleaf(String id,String childrentable,String pid){
    	String [] myid=id.split("_");
        id=myid[myid.length-1];
    	boolean b=true;
    	DAO dao=new DAO();
    	String sql="select count(1) from "+childrentable+" where "+pid+"="+id;
    	try{
    	String itotal=dao.getDataValue(ConstUtil.DBNAME, sql);
    	int irows=Integer.valueOf(itotal);
    	if(irows>0){
    	b=false;	
    	}
    	}catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    	
    	return b;
    	
    }
    }

